package main

import "fmt"

func nonInterfaceError() {
	name := "Jack"
	fmt.Printf("%T\n", name)
	//string 非 interface
	//err Invalid type assertion: name.(string) (non-interface type string on the left)
	//s, ok := name.(string)
	//if ok {
	//	fmt.Printf("%q\n",s)
	//}else{
	//	fmt.Println("value is not a string")
	//}
}

func stringInterface() {
	var name interface{} = "Jack"
	s, err := name.(string)
	if err {
		fmt.Printf("%T\n", s)
		fmt.Printf("%v\n", s)
	} else {
		fmt.Println("name is not string")
	}
}

func intInterfaceAssertFail() {
	var name interface{} = 7
	s, err := name.(string)
	if err {
		fmt.Printf("%T\n", s)
		fmt.Printf("%v\n", s)
	} else {
		fmt.Println("name is not string")
		fmt.Printf("%T\n", s)
		fmt.Printf("%v\n", s)
	}
}

func interfaceIntSum() {
	var val interface{} = 8
	//fmt.Println(val + 10) //不能编译
	fmt.Println(val.(int) + 10)
}

func castingReminder() {
	rem := 7.25
	fmt.Printf("%T\n", rem)
	fmt.Printf("%T\n", int(rem))
}

func main() {
	stringInterface()
	fmt.Println("=============================")
	intInterfaceAssertFail()
	fmt.Println("=============================")
	interfaceIntSum()
	fmt.Println("=============================")
	castingReminder()
	fmt.Println("=============================")

}
